﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using  Print=JTicket.Printer.Print;
using System.Web.Script.Services;
using System.Text;
using System.Data;
using System.IO;
namespace JTicket.UI.Service
{
    /// <summary>
    /// Summary description for JT_TicketOperateSvc
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    [GenerateScriptType(typeof(JTicket.Model.SaleParm))]
    public class JT_TicketOperateSvc : System.Web.Services.WebService
    {
        /// <summary>
        /// 销售
        /// </summary>
        /// <param name="saleParm"></param>
        /// <returns></returns>
        [WebMethod]
        public string SaleTicket(JTicket.Model.SaleParm saleParm)
        {
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            BLL.D_Tax bllTax = new BLL.D_Tax();
            Model.D_OperInfo oper = bllOper.Oper();
            string thcode = oper.ThCode;
            string Invodio = "无";
            //销售前的系统状态判断。(登录状态,发票剩余状态)
            if (!User.Identity.IsAuthenticated)
                return "0";//你的账号还未登录，系统终止售票业务。
            if (BLL.D_Tax.Exists(thcode))
            {
                Model.D_Tax Tax = bllTax.GetModel(thcode);
                if (Tax.fpsy.Equals(Tax.fpsz))
                    return "1";//发票已经用完,系统终止售票业务。";
                else
                    Invodio = Tax.fpsy;
            }
            //数据库销售业务。
            string opercode = oper.OperCode;
            string saleip = HttpContext.Current.Request.UserHostAddress;
            string barcode = Common.CheckCode.ListBarCode(1).First();
            string Flag = Common.StringHelp.GenerateFileName();
            System.Threading.Thread.Sleep(100);
            bool b = BLL.H_TicketBal.Ticket_Sale(saleParm, opercode, saleip, barcode, Invodio, Flag);
            if (b)
            {
                //生成发票号。
                if (Invodio != "无")
                {
                    BuildTaxU(saleParm.SumAmt.ToString(), saleParm.UserName, saleParm.SumCount);
                }
                //客户端打印。
                return Print.MakeSaleCmd(barcode, saleParm.Sixid, saleParm.TitleCode, saleParm.ThCode);
            }
            return "2";//销售错误,系统终止售票业务。
        }
        /// <summary>
        /// 预留
        /// </summary>
        /// <param name="saleParm"></param>
        /// <returns></returns>
        [WebMethod]
        public string BookTicket(JTicket.Model.SaleParm saleParm)
        {

            return "预留成功！";
        }
        /// <summary>
        /// 预订
        /// </summary>
        /// <param name="saleParm"></param>
        /// <returns></returns>
        [WebMethod]
        public string OrderTicket(JTicket.Model.SaleParm saleParm)
        {
            return "预订成功！";
        }
        /// <summary>
        /// 预留解锁
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string BookUnLock()
        {
            return "预留解锁成功！";
        }
        /// <summary>
        /// 预订取票
        /// </summary>
        /// <param name="OrderID">订单号</param>
        /// <param name="OrderUserName">预订人姓名</param>
        /// <param name="OrderUserTel">预订人电话</param>
        /// <returns></returns>
        [WebMethod]
        public string OrderGetTicket(string OrderID)
        {
            BLL.D_Tax bllTax = new BLL.D_Tax(); 
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            Model.D_Tax Tax = bllTax.GetModel(bllOper.Oper().ThCode);
            StringBuilder sb = new StringBuilder();
            if (!User.Identity.IsAuthenticated)
                return "0";//你的账号还未登录，系统终止预订取票业务。";
            if (Tax.fpsy.Equals(Tax.fpsz))
                return "1";//发票已经用完,系统终止预订取票业务。";
            //获取订单明细。
            decimal SumAmt = 0.0M;
            string UserName = string.Empty;
            int SumCount = 0;
            //数据库销售业务。
            bool b = true;
            if (b)
            {
                List<JTicket.Model.H_OrderBal> OrderList = new List<Model.H_OrderBal>();
                foreach (var order in OrderList)
                {
                    //客户端打印。
                    //sb.Append(Print.MakeOrderGetCmd(order.ordercode,
                        //order.concertcode, order.ThCode));
                    SumAmt += SumAmt;
                    SumCount += SumCount;
                    UserName = order.usertxt;
                }
                //生成发票号。
                BuildTaxU(SumAmt.ToString(), UserName, SumCount);
                return sb.ToString();//返回打印SQL命令集。
            }
            return "2";//"系统处理错误,系统终止预订取票业务。";
        }
        /// <summary>
        /// 退订
        /// </summary>
        /// <param name="OrderID">
        public string OrderCancel(string BarCode, string Row, string Cow, string Area, string TitleCode, string OrderID)
        {
            //取消操作。
            bool b = true;
            if (b)
            {
                return "订单取消成功！";
            }
            return "订单取消失败,系统终止取消业务。";
        }
        /// <summary>
        /// 申请重打印
        /// </summary>
        /// <param name="BarCode"></param>
        /// <param name="TitleCode"></param>
        /// <returns></returns>
        public string RegitPrint(string BarCode, string TitleCode)
        {
            //判断是否审核过、
            //if ()
            //return "1";   
            //数据库销售业务。
            bool b = true;
            if (b)
            {
                //生成发票号。
                return "重打印申请成功,请等待管理员的申请！";
            }
            //销售错误,系统终止售票业务。
            return "2";
        }
        /// <summary>
        /// 重打印
        /// </summary>
        /// <param name="BarCode"></param>
        /// <param name="TitleCode"></param>
        /// <returns></returns>
        [WebMethod]
        public string RePrint(string BarCode, string Row, string Cow, string Area, string TitleCode)
        {

            //判断是否审核过、
            //if ()
            //return "1";   
            //数据库重打票业务。
            bool b = true;
            if (b)
            {
                //生成发票号。

                //客户端打印。
                return Print.MakeSaleCmd("123123", "12", "aa", "001");
            }
            //销售错误,系统终止售票业务。
            return "2";


        }
        /// <summary>
        /// 退票
        /// </summary>
        /// <param name="BarCode"></param>
        /// <param name="Row"></param>
        /// <param name="Cow"></param>
        /// <param name="TitleCode"></param>
        /// <returns></returns>
        [WebMethod]
        public string ReturnTicket(List<string[]> listStrArr)
        {
            //数据库退票业务。
            bool b = true;
            if (b)
                return "退票操作成功！";
            return "退票失败,系统终止退票业务";
        }
        public void BuildTaxU(string kpje, string kpr, int count)
        {
            BLL.D_Tax bllTax = new BLL.D_Tax(); 
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo(); 
            Model.D_Tax Tax = bllTax.GetModel(bllOper.Oper().ThCode);
            try
            {
                if (string.IsNullOrEmpty(kpr))
                    kpr = "无";
                string FileName = string.Format("{0}{1}{2}{3}",
                    DateTime.Now.ToString("yyyyMMdd"), "U", Tax.swbh, ".xml");
                string FilePath = HttpContext.Current.Server.MapPath("XmlFile\\" + FileName + "");
                StringBuilder sb = new StringBuilder();
                DataSet dataSet = new DataSet();
                DataRow dr;
                dataSet.ReadXmlSchema(HttpContext.Current.Server.MapPath("App_Data\\InvoiceUseInfo.xsd"));
                if (File.Exists(FilePath))
                    dataSet.ReadXml(FilePath);
                //表ROOT
                dr = dataSet.Tables["ROOT"].NewRow();
                dr["ROOT_Id"] = Tax.fpsy;
                dataSet.Tables["ROOT"].Rows.Add(dr);
                //表Nsritem
                dr = dataSet.Tables["Nsritem"].NewRow();
                dr["Nsrsbh"] = Tax.swbh;
                dr["Nsrmc"] = Tax.qymc;
                dr["Ssqq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Ssqz"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Rq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Nsritem_Id"] = Tax.fpsy;
                dr["ROOT_Id"] = Tax.fpsy;
                dataSet.Tables["Nsritem"].Rows.Add(dr);
                //表Fplist
                dr = dataSet.Tables["Fplist"].NewRow();
                dr["Fplist_Id"] = Tax.fpsy;
                dr["Nsritem_Id"] = Tax.fpsy;
                dataSet.Tables["Fplist"].Rows.Add(dr);
                //======================================================
                //表Fpinfo
                dr = dataSet.Tables["Fpinfo"].NewRow();
                dr["InitFpdm"] = Tax.Fpdm;
                dr["Fpdm"] = Tax.Fpdm;
                dr["Fpdmext"] = Tax.Fpdm;
                string st = Tax.fpsy;
                //更新当前使用发票代码
                BLL.D_Tax.UpdateSY(bllOper.Oper().ThCode);
                dr["Fphm"] = st;
                dr["Zfbz"] = "0";
                dr["Kprq"] = DateTime.Now.ToString("yyyy-MM-dd");
                dr["Kpje"] = kpje;
                dr["Kpr"] = kpr;
                dr["Fpinfo_Id"] = Tax.fpsy;
                dr["Fplist_Id"] = Tax.fpsy;
                dataSet.Tables["Fpinfo"].Rows.Add(dr);
                //表Fkf
                dr = dataSet.Tables["Fkf"].NewRow();
                dr["Fkfnsrsbh"] = "无";
                dr["Fkfnrsmc"] = Tax.qymc;
                dr["Fkfaddr"] = "无";
                dr["Fpinfo_Id"] = Tax.fpsy;
                dataSet.Tables["Fkf"].Rows.Add(dr);
                //表Skf
                dr = dataSet.Tables["Skf"].NewRow();
                dr["Skfnsrsbh"] = Tax.swbh;
                dr["Skfnrsmc"] = Tax.qymc;
                dr["Skr"] = kpr; ;
                dr["Fpinfo_Id"] = Tax.fpsy;
                dataSet.Tables["Skf"].Rows.Add(dr);
                dataSet.Tables["Fplist"].AcceptChanges();
                dataSet.Tables["Fpinfo"].AcceptChanges();
                dataSet.Tables["Fkf"].AcceptChanges();
                dataSet.Tables["Skf"].AcceptChanges();
                dataSet.AcceptChanges();
                //======================================================
                sb.Append("<?xml version=\"1.0\"  encoding=\"GBK\" standalone=\"yes\"?>");
                sb.Append(dataSet.GetXml());
                File.WriteAllText(FilePath, sb.ToString(), Encoding.Default);
            }
            catch
            {

            }
            //dataSet.WriteXml(FilePath);
        }
        public void BuildTaxS(string kpje, string kpr, int count)
        {
            BLL.D_Tax bllTax = new BLL.D_Tax(); 
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo(); 
            Model.D_Tax Tax = bllTax.GetModel(bllOper.Oper().ThCode);
            //纳税人识别号
            string FileName = string.Format("{0}{1}{2}{3}", DateTime.Now.ToString("yyyyMMdd"), "S", Tax.swbh, ".xml");
            string FilePath = HttpContext.Current.Server.MapPath("Xml\\" + FileName + "");
            StringBuilder sb = new StringBuilder();
            DataSet dataSet = new DataSet();
            DataRow dr;
            dataSet.ReadXmlSchema(HttpContext.Current.Server.MapPath("App_Data\\InvoiceSaleInfo.xsd"));
            //表一
            dr = dataSet.Tables[0].NewRow();
            dr["Nsrsbh"] = Tax.swbh;
            dr["Nsrmc"] = Tax.qymc;
            dr["SaleDate"] = DateTime.Now.ToString("yyyy-MM-dd");
            dr["Fpjbr"] = "无";
            dr["Swjbrdm"] = Tax.Swjbrdm; ;
            dr["Swjbrmc"] = Tax.Swjbrmc;
            dr["Ckkfdm"] = "03900001";
            dr["Ckkfmc"] = "税务局直属分局发票窗口1";
            dr["FpSaleInfo_Id"] = "2";
            dataSet.Tables[0].Rows.Add(dr);
            //表二
            dr = dataSet.Tables[1].NewRow();
            dr["SaleInfo_Id"] = "1";
            dr["FpSaleInfo_Id"] = "2";
            dataSet.Tables[1].Rows.Add(dr);

            //表三
            //for (int i = 0; i <= 1; i++)
            //{
            dr = dataSet.Tables[2].NewRow();
            dr["Fpmcdm"] = "037515221006000000783224";
            dr["Fpmc"] = Tax.Fpmc;
            dr["Fplbdm"] = "1";
            dr["Fplbmc"] = "其它";
            dr["Fpdm"] = "30000001";
            dr["Fpdmext"] = "221010659014-4";
            dr["Fpqh"] = "41205551";
            dr["Fpzh"] = "41206550";
            dr["Mbzs"] = "50";
            dr["Dw"] = "张";
            dr["Bs"] = "2";
            dr["Dj"] = "150";
            dr["Je"] = "300";
            dr["SaleInfo_Id"] = "1";
            dataSet.Tables[2].Rows.Add(dr);
            //}
            sb.Append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\"?>");
            sb.Append(dataSet.GetXml().Replace("<NewDataSet>", "").Replace("</NewDataSet>", ""));
            if (!File.Exists(FilePath))
            {
                File.WriteAllText(FilePath, sb.ToString(), Encoding.Default);
            }
        }


        #region

        [WebMethod]
        public string GetSQL()
        {   
           //return string.Format("{0}{1}", "Provider=SQLOLEDB.1;",
                //JTicket.Config.DBConfig.ApplicationString);
            return string.Format("{0}{1}", "Provider=SQLOLEDB.1;",JTicket.Config.UIConfig.WebConnection);
        }

        [WebMethod]
        public string DesingTicket()
        {
            return JTicket.Printer.Print.MakePrintModel();
        }
        #endregion
    }
}
